<?php
class MarkersController extends Zend_Controller_Action
{

    public function init()
    {
        $this->_helper->layout->setLayout('layoutadmin');
    }

    public function indexAction()
    {
        // action body
    }

    public function addmarkerAction()
    {
        $form = new Application_Form_Addmarker();
        $this->view->form = $form;     
    }

    public function savemarkersAction()
    {
        
        $data = array(
            'name'        => $this->getRequest()->getPost('name'),
            'address'     => $this->getRequest()->getPost('description'),
            'lng'         => $this->getRequest()->getPost('lng'),
            'lat'         => $this->getRequest()->getPost('lat'),
            'type'        => $this->getRequest()->getPost('type')
        );
     
        $add = new Application_Model_Markers();
        $add->addMarkers($data);          
    }

    public function addcategoryAction()
    {
        $form = new Application_Form_Category();
        $this->view->form = $form;
        $add = new Application_Model_Markers();
        
        
        //проверяем был ли пост запрос
        if ($this->getRequest()->isPost()) {
             //если был - получаем его и проверяем на валидность
             $postData = $this->getRequest()->getPost();
             if ($form->isValid($postData)) {
                $file = $form->icon->getFileInfo();
                $ext = pathinfo($file['icon']['name'], PATHINFO_EXTENSION);
                //генерируем имя иконки
                $newName = rand().".".$ext;
                

                $form->icon->addFilter('Rename', realpath(dirname('.')).
                DIRECTORY_SEPARATOR.
                'uploads/category'.
                DIRECTORY_SEPARATOR.
                $newName);

                $form->icon->receive(); 

                 $data = array(
                     'name' => $form->getvalue('name'),
                     'icon' => $newName
                 );    
             $add->addCategory($data); 
             $this->_helper->redirector('allcategories','markers');
             }
        }   
    }

    public function allcategoriesAction()
    {
        //делаем выборку всех категори и передаем в вид
        $get = new Application_Model_Markers();
        $cat = $get->getAllcategories();
        $this->view->cat = $cat;
    }

    public function deletecategoryAction()
    {
        $id = $this->_getParam('id', 0);
        $del = new Application_Model_Markers();
        $del->deleteCategory($id);
        $this->_helper->redirector('allcategories', 'markers');
    }

    public function updatecategoryAction()
    {
        $id = $this->_getParam('id', 0);
         //выводим форму
        $form = new Application_Form_Category();
        $this->view->form = $form;
        //делаем выборку данных указанного юзера
        //и заполняем ими форму
        $update = new Application_Model_Markers();
        $form->populate($update->getSelectCategory($id));
        if ($this->getRequest()->isPost()) {
            $postdata = $this->getRequest()->getPost();
            if ($form->isValid($postdata)) {
                
                $file = $form->icon->getFileInfo();
                $ext = pathinfo($file['icon']['name'], PATHINFO_EXTENSION);
                //генерируем имя иконки
                $newName = rand().".".$ext;
                

                $form->icon->addFilter('Rename', realpath(dirname('.')).
                DIRECTORY_SEPARATOR.
                'uploads/category'.
                DIRECTORY_SEPARATOR.
                $newName);

                $form->icon->receive(); 
                
                $data = array(
                              'name'  => $form->getValue('name'),
                              'icon' => $newName
                        );
                $update->updateCategory($id, $data);
                $this->_helper->redirector('allcategories','markers');
            }       
        }        
    }

    public function allmarkersAction()
    {
        //делаем выборку всех маркеров
        $get = new Application_Model_Markers();
        $all = $get->getAllMarkers();
        $this->view->all = $all;       
    }

    public function deletemarkerAction()
    {
        $id = $this->_getParam('id', 0);
        $del = new Application_Model_Markers();
        $del->deleteMarker($id);
        $this->_helper->redirector('allmarkers', 'markers');
    }

    public function updatemarkerAction()
    {
        $id = $this->_getParam('id', 0);
        $form = new Application_Form_Updatemarker();
        $this->view->form = $form;
        $update = new Application_Model_Markers();
       // $form->populate($update->getSelectMarker($id));

    }

    public function xmlAction()
    {
        $obj = new Application_Model_Markers();
        //получаем все категории
        $table = $obj->getAllcategories();
        
        $this->view->table = $table;
        
    }

    public function saveupdatemarkerAction()
    {
        $id = 68; //$this->_getParam('id', 0);
      /*   $data = array(
            'name'        => $this->getRequest()->getPost('name'),
            'address'     => $this->getRequest()->getPost('description'),
            'lng'         => $this->getRequest()->getPost('lng'),
            'lat'         => $this->getRequest()->getPost('lat'),
            'type'        => $this->getRequest()->getPost('type')
        );
     */
        $data = array();
        $update = new Application_Model_Markers();
        $update->updateMarker($id, $data);  
           
    }

    public function markerboxAction()
    {
        // action body
    }


}



